package com.lego.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.lego.bean.Administrator;
import com.lego.dao.AdminDao;
import com.lego.dao.BaseDao;

public class AdminDaoImpl implements AdminDao {

	@Override
	public Administrator selectAdmin(String adName, String adPwd) {
		Connection con=null;
		
		PreparedStatement ps=null;
		ResultSet res=null;
		Administrator admin=null;
		String sql="SELECT * FROM administrator WHERE adminName=? AND adminPwd=?";
		
		try {
			con=BaseDao.getCon();
			ps=con.prepareStatement(sql);
			ps.setString(1, adName);
			ps.setString(2, adPwd);
			res=ps.executeQuery();
			if(res.next()) {
				admin=new Administrator();
				admin.setAdminId(res.getInt("adminid"));
				admin.setAdminName(res.getString("adminName"));
				admin.setAdminPwd(res.getString("adminPWd"));
				admin.setRealName(res.getString("RealName"));
				admin.setCreateTime(res.getDate("createTime"));
				admin.setLastLoginTime(res.getDate("LastLoginTime"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(con, ps, res);
		}
		return admin;
	}

	@Override
	public Administrator selectAdminByName(String adminName) {
		Connection con=null;
		PreparedStatement ps=null;
		ResultSet res=null;
		Administrator admin=null;
		String sql= "SELECT * FROM administrator WHERE adminName=?";
		try {
			con=BaseDao.getCon();
			ps=con.prepareStatement(sql);
			ps.setString(1, adminName);
			res=ps.executeQuery();
			if(res.next()) {
				admin=new Administrator();
				admin.setAdminId(res.getInt("adminid"));
				admin.setAdminName(res.getString("adminName"));
				admin.setAdminPwd(res.getString("adminPWd"));
				admin.setRealName(res.getString("RealName"));
				admin.setCreateTime(res.getDate("createTime"));
				admin.setLastLoginTime(res.getDate("LastLoginTime"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(con, ps, res);
		}
		return admin;
	}

}
